<?php

namespace App\Http\Controllers\Auth;

use App\Helpers\CustomerValidator;
use App\Http\Controllers\Controller;
use App\Models\Code;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class RegisterController extends Controller
{
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'platform' => 'required',
        ], [
            'platform.required' => '设备不能为空',
        ]);

        if ($validator->fails()) {
            return jsonResponse(config('common.validatorError'), $validator);
        }
        $platform = $request->get('platform');
        if (in_array($platform, config('common.platform.auth2'))) {
            // 第三方注册
            # TODO
            // 微信登录部分
            return '功能暂未开放';
        } else {
            // 1：邮箱，2：手机号
            $type = $request->get('type', 1);
            $token = hash('sha256', Str::random(60));
            $agent = $request->header('user-agent');
            switch ($type) {
                case 1: {
                    $validator = Validator::make($request->all(), [
                        'email' => 'email',
                    ], [
                        'email' => '邮箱不合法',
                    ]);
                    if ($validator->fails()) {
                        return jsonResponse(config('common.validatorError'), $validator);
                    }
                    $email = $request->get('email');
                    $user = User::where('email', $email)->first();
                    if (!empty($user)) {
                        return jsonResponse(41006);
                    }
                    $password = $request->get('password');
                    $confirmPwd = $request->get('confirmPwd');
                    if ($password == $confirmPwd) {
                        $user = new User();
                        $user['name'] = time();
                        $user['email'] = $email;
                        $user['password'] = bcrypt($password);
                        $user['token'] = $token;
                        $user['last_ip'] = $request->getClientIp();
                        $user['last_time'] = time();
                        $user->save();
                        setLoginLog($user, 3, $agent);
                        return jsonResponse(config('common.successCode'), $user);
                    } else {
                        return jsonResponse(41005);
                    }
                }
                case 2: {
                    $mobile = $request->get('mobile');
                    $valid = new CustomerValidator();
                    if ($valid->checkMobile($mobile)) {
                        $number = $request->get('code');
                        $code = Code::where('device', $mobile)->where('code', $number)->first();
                        if (empty($code)) {
                            return jsonResponse(41007);
                        } else {
                            $user = User::where('phone', $mobile)->first();
                            if (empty($user)) {
                                $user = new User();
                                $user['phone'] = $mobile;
                                $user['phone_verified_at'] = date('Y-m-d H:i:s');
                            }
                            $user['token'] = $token;
                            $user['last_ip'] = $request->getClientIp();
                            $user['last_time'] = time();
                            $user->save();
                            setLoginLog($user, 3, $agent);
                            return jsonResponse(config('common.successCode'), $user);
                        }
                    } else {
                        return jsonResponse(41003);
                    }
                }
                default: return jsonResponse(40001);
            }
        }
    }
}
